package org.file5.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.file5.domain.Article;
import org.file5.domain.Attachment;
import org.file5.domain.Category;
import org.file5.service.ServiceFascade;
import org.file5.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.googlecode.objectify.Key;

@Controller
@RequestMapping("/admin/article/edit")
@SessionAttributes("article")
public class EditArticleForm {
	private final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private ServiceFascade sf;

	@InitBinder
	public void setAllowedFields(WebDataBinder dataBinder) {
		dataBinder.setDisallowedFields(new String[] { "uid" });
	}

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm(@RequestParam("uid") String uid, ModelMap model) {
		Article article = this.sf.getArticleService().find(uid,true);
		model.addAttribute("article", article);
		if (article.getPrimaryCategoryKey() != null) {
			Category g = sf.getBaseService().find(
					article.getPrimaryCategoryKey());
			if (g != null) {
				model.addAttribute("currentCategory", g);
				List<Category> primaryCategoryTrail = sf.getCategoryService()
						.getPrimaryCategoryTrail(
								new Key<Category>(Category.class, g.getUid()));
				model
						.addAttribute("primaryCategoryTrail",
								primaryCategoryTrail);
			}

		}


		return "admin/article/articleedit";
	}

	@RequestMapping(method = { RequestMethod.PUT, RequestMethod.POST })
	public String processSubmit(@ModelAttribute("article") Article article,
			BindingResult result, SessionStatus status,
			HttpServletRequest request) {

		// new PetValidator().validate(article, result);
		if (result.hasErrors()) {
			return "admin/article/articleedit";
		} else {
			if(article.getMultipartFile()!=null&&!article.getMultipartFile().isEmpty()){
				Attachment attachment = new Attachment();
				attachment.setUid( UUIDUtil.getRandomUUID());
				attachment.setName(article.getName()+"小图");
				attachment.setMultipartFile(article.getMultipartFile());
				attachment = sf.getAttachmentService().update(attachment);			
			}
			article = sf.getBaseService().addUpdate(article);
			
			sf.getEntityIndexService().addIndexTask(Article.class, article.getUid());
			String categoryUid = article.getPrimaryCategoryKey()==null?"":article.getPrimaryCategoryKey().getName();
			return "redirect:/admin/article/list.asp?guid="+categoryUid;
		}
	}
}
